<?php
include_once 'CardFactory.php';
class Table
{
	var $tableID = "";
	var $tableName = "";
	var $createrID = "";
	var $maxUserNum = 8;
	var $start = false;
	var $currentPlayerSeatID = "";
	var $seatList = array();
	var $cardSet = array();
	var $direction = "next";
	var $currentCard = "";
	var $rollTime = true;
	var $cardState = null;
	var $gameOver = false;

	function Table()
	{
		$this->cardState = new CardState();
	}

	function __toString()
	{
		return '{"tableID":"'.$this->tableID.'","tableName":"'.$this->tableName.'","createrID":"'.$this->createrID.'","userNum":"'.count($this->seatList).'","maxUserNum":"'.$this->maxUserNum.'","start":"'.$this->start.'","rollTime":"'.$this->rollTime.'"}';
	}

	function getUserList()
	{
		$ul = array();
		foreach($this->seatList as $seat)
		{
			if(!empty($seat->userID)||$seat->userID=='0'){
				array_push($ul,$seat->userID);
			}
		}
		return $ul;
	}

	function standUp($seatID)
	{
		if(count($this->seatList)>1)
		{
			if($this->seatList[$seatID]->userID == $this->createrID)
			{
				foreach($this->seatList as $seat)
				{
					if($seat->seatID==$seatID)
					{
						continue;
					}else{
						$this->createrID = $seat->userID;
						UserCenter::getInstance()->userList[$seat->userID]->ready = true;
						break;
					}
				}
			}
		}else
		{
			$this->createrID = "";
		}

		unset($this->seatList[$seatID]);
		return $this->seatList;
	}

	function getDetail()
	{
		$temp = "{";
		foreach($this->seatList as $seat)
		{
			$temp .= '"'.$seat->seatID.'":'.$seat.",";
		}

		$temp .="}";
		$temp = str_replace(",}","}",$temp);
		return '{"tableID":"'.$this->tableID.'","tableName":"'.$this->tableName.'","createrID":"'.$this->createrID.'","userList":'.$temp.',"maxUserNum":"'.$this->maxUserNum.'","start":"'.$this->start.'"}';
	}

	function start()
	{
		$this->start = true;
		$this->cardSet = CardFactory::getInstance()->getNextCardSet();

		$activeSeatList = array();
		foreach($this->seatList as $k=>$v)
		{
			array_push($activeSeatList,$k);
		}
		//随机位置
		$len=round(count($activeSeatList)/2);
		for($i=0;$i<$len;$i++)
		{
			$si = rand(0,$len-1);
			$fi = rand(0,$len-1);
			if($si != $fi)
			{
				$str = $activeSeatList[$si];
				$activeSeatList[$si] = $activeSeatList[$fi];
				$activeSeatList[$fi] = $str;
			}
		}

		//设置座位并发牌
		$len=count($activeSeatList);
		for($i=0;$i<$len;$i++)
		{
			$this->seatList[$activeSeatList[$i]]->nextSeatID = $this->seatList[ $activeSeatList[($i+1) % $len] ]->seatID;
			$this->seatList[$activeSeatList[$i]]->prevSeatID = $this->seatList[ $activeSeatList[($i+$len-1)%$len] ]->seatID;
			for($j=0;$j<4;$j++)
			{
				array_push($this->seatList[$activeSeatList[$i]]->cardList,array_pop($this->cardSet));
			}
		}
		$this->currentPlayerSeatID = $activeSeatList[0];
	}

	function popCard()
	{
		$c = array_pop($this->cardSet);

		if(empty($this->cardSet))
		{
			$this->cardSet = CardFactory::getInstance()->getNextCardSet();
		}

		return $c;
	}

	function getCardInfo($seatID)
	{
		$config = '{';
		foreach($this->seatList as $k=>$v)
		{
			if($k == $seatID)
			{
				$config .= '"'.$k.'":{"status":"","nextSeatID":"'.$v->nextSeatID.'","cards":{';

				foreach($v->cardList as $kk=>$vv)
				{
					$config .='"'.$kk.'":"'.$vv.'",';
				}
				$config .= '}},';
				$config = str_replace(",}","}",$config);
			}else{
				$config .= '"'.$k.'":{"userID":"'.$v->userID.'","cards":"'.count($this->seatList[$k]->cardList).'","status":"","nextSeatID":"'.$v->nextSeatID.'"},';
			}
		}
		$config .= '}';
		$config = str_replace(",}","}",$config);

		return '{"tableID":"'.$this->tableID.'","currentSeatID":"'.$this->currentPlayerSeatID.'","direction":"'.$this->direction.'","cardNum":"'.count($this->cardSet).'","config":'.$config.'}';
	}
	
	function getResult()
	{
		echo "======================================================\n";
		print_r($this->seatList);
		$rs = '{';
		foreach($this->seatList as $k=>$v)
		{
			$rs .= '"'.$k.'":["'.implode("\",\"",$v->cardList).'"],';
		}
		$rs = substr($rs, 0, -1);
		echo $rs;
		echo "\n======================================================\n";
		return $rs.'}';
	}
	
	function getPrevPlayer()
	{
		
	}
}//end of class Table